home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / Modules / evt / Evtmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-31  |  9.9 KB  |  427 lines  |  [TEXT/CWIE]

  1.  
  2. /* =========================== Module Evt =========================== */
  3.  
  4. #include "Python.h"
  5.  
  6.  
  7.  
  8. #define SystemSevenOrLater 1
  9.  
  10. #include "macglue.h"
  11. #include <Memory.h>
  12. #include <Dialogs.h>
  13. #include <Menus.h>
  14. #include <Controls.h>
  15.  
  16. extern PyObject *ResObj_New(Handle);
  17. extern int ResObj_Convert(PyObject *, Handle *);
  18. extern PyObject *OptResObj_New(Handle);
  19. extern int OptResObj_Convert(PyObject *, Handle *);
  20.  
  21. extern PyObject *WinObj_New(WindowPtr);
  22. extern int WinObj_Convert(PyObject *, WindowPtr *);
  23. extern PyTypeObject Window_Type;
  24. #define WinObj_Check(x) ((x)->ob_type == &Window_Type)
  25.  
  26. extern PyObject *DlgObj_New(DialogPtr);
  27. extern int DlgObj_Convert(PyObject *, DialogPtr *);
  28. extern PyTypeObject Dialog_Type;
  29. #define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type)
  30.  
  31. extern PyObject *MenuObj_New(MenuHandle);
  32. extern int MenuObj_Convert(PyObject *, MenuHandle *);
  33.  
  34. extern PyObject *CtlObj_New(ControlHandle);
  35. extern int CtlObj_Convert(PyObject *, ControlHandle *);
  36.  
  37. extern PyObject *GrafObj_New(GrafPtr);
  38. extern int GrafObj_Convert(PyObject *, GrafPtr *);
  39.  
  40. extern PyObject *BMObj_New(BitMapPtr);
  41. extern int BMObj_Convert(PyObject *, BitMapPtr *);
  42.  
  43. extern PyObject *WinObj_WhichWindow(WindowPtr);
  44.  
  45. #include <Events.h>
  46. #include <Desk.h>
  47.  
  48. #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
  49.  
  50. static PyObject *Evt_Error;
  51.  
  52. static PyObject *Evt_GetCaretTime(_self, _args)
  53.     PyObject *_self;
  54.     PyObject *_args;
  55. {
  56.     PyObject *_res = NULL;
  57.     UInt32 _rv;
  58.     if (!PyArg_ParseTuple(_args, ""))
  59.         return NULL;
  60.     _rv = GetCaretTime();
  61.     _res = Py_BuildValue("l",
  62.                          _rv);
  63.     return _res;
  64. }
  65.  
  66. static PyObject *Evt_SetEventMask(_self, _args)
  67.     PyObject *_self;
  68.     PyObject *_args;
  69. {
  70.     PyObject *_res = NULL;
  71.     EventMask value;
  72.     if (!PyArg_ParseTuple(_args, "h",
  73.                           &value))
  74.         return NULL;
  75.     SetEventMask(value);
  76.     Py_INCREF(Py_None);
  77.     _res = Py_None;
  78.     return _res;
  79. }
  80.  
  81. static PyObject *Evt_GetDblTime(_self, _args)
  82.     PyObject *_self;
  83.     PyObject *_args;
  84. {
  85.     PyObject *_res = NULL;
  86.     UInt32 _rv;
  87.     if (!PyArg_ParseTuple(_args, ""))
  88.         return NULL;
  89.     _rv = GetDblTime();
  90.     _res = Py_BuildValue("l",
  91.                          _rv);
  92.     return _res;
  93. }
  94.  
  95. static PyObject *Evt_GetNextEvent(_self, _args)
  96.     PyObject *_self;
  97.     PyObject *_args;
  98. {
  99.     PyObject *_res = NULL;
  100.     Boolean _rv;
  101.     EventMask eventMask;
  102.     EventRecord theEvent;
  103.     if (!PyArg_ParseTuple(_args, "h",
  104.                           &eventMask))
  105.         return NULL;
  106.     _rv = GetNextEvent(eventMask,
  107.                        &theEvent);
  108.     _res = Py_BuildValue("bO&",
  109.                          _rv,
  110.                          PyMac_BuildEventRecord, &theEvent);
  111.     return _res;
  112. }
  113.  
  114. static PyObject *Evt_WaitNextEvent(_self, _args)
  115.     PyObject *_self;
  116.     PyObject *_args;
  117. {
  118.     PyObject *_res = NULL;
  119.     Boolean _rv;
  120.     EventMask eventMask;
  121.     EventRecord theEvent;
  122.     UInt32 sleep;
  123.     if (!PyArg_ParseTuple(_args, "hl",
  124.                           &eventMask,
  125.                           &sleep))
  126.         return NULL;
  127.     _rv = WaitNextEvent(eventMask,
  128.                         &theEvent,
  129.                         sleep,
  130.                         (RgnHandle)0);
  131.     _res = Py_BuildValue("bO&",
  132.                          _rv,
  133.                          PyMac_BuildEventRecord, &theEvent);
  134.     return _res;
  135. }
  136.  
  137. static PyObject *Evt_EventAvail(_self, _args)
  138.     PyObject *_self;
  139.     PyObject *_args;
  140. {
  141.     PyObject *_res = NULL;
  142.     Boolean _rv;
  143.     EventMask eventMask;
  144.     EventRecord theEvent;
  145.     if (!PyArg_ParseTuple(_args, "h",
  146.                           &eventMask))
  147.         return NULL;
  148.     _rv = EventAvail(eventMask,
  149.                      &theEvent);
  150.     _res = Py_BuildValue("bO&",
  151.                          _rv,
  152.                          PyMac_BuildEventRecord, &theEvent);
  153.     return _res;
  154. }
  155.  
  156. static PyObject *Evt_GetMouse(_self, _args)
  157.     PyObject *_self;
  158.     PyObject *_args;
  159. {
  160.     PyObject *_res = NULL;
  161.     Point mouseLoc;
  162.     if (!PyArg_ParseTuple(_args, ""))
  163.         return NULL;
  164.     GetMouse(&mouseLoc);
  165.     _res = Py_BuildValue("O&",
  166.                          PyMac_BuildPoint, mouseLoc);
  167.     return _res;
  168. }
  169.  
  170. static PyObject *Evt_Button(_self, _args)
  171.     PyObject *_self;
  172.     PyObject *_args;
  173. {
  174.     PyObject *_res = NULL;
  175.     Boolean _rv;
  176.     if (!PyArg_ParseTuple(_args, ""))
  177.         return NULL;
  178.     _rv = Button();
  179.     _res = Py_BuildValue("b",
  180.                          _rv);
  181.     return _res;
  182. }
  183.  
  184. static PyObject *Evt_StillDown(_self, _args)
  185.     PyObject *_self;
  186.     PyObject *_args;
  187. {
  188.     PyObject *_res = NULL;
  189.     Boolean _rv;
  190.     if (!PyArg_ParseTuple(_args, ""))
  191.         return NULL;
  192.     _rv = StillDown();
  193.     _res = Py_BuildValue("b",
  194.                          _rv);
  195.     return _res;
  196. }
  197.  
  198. static PyObject *Evt_WaitMouseUp(_self, _args)
  199.     PyObject *_self;
  200.     PyObject *_args;
  201. {
  202.     PyObject *_res = NULL;
  203.     Boolean _rv;
  204.     if (!PyArg_ParseTuple(_args, ""))
  205.         return NULL;
  206.     _rv = WaitMouseUp();
  207.     _res = Py_BuildValue("b",
  208.                          _rv);
  209.     return _res;
  210. }
  211.  
  212. static PyObject *Evt_GetKeys(_self, _args)
  213.     PyObject *_self;
  214.     PyObject *_args;
  215. {
  216.     PyObject *_res = NULL;
  217.     KeyMap theKeys__out__;
  218.     if (!PyArg_ParseTuple(_args, ""))
  219.         return NULL;
  220.     GetKeys(theKeys__out__);
  221.     _res = Py_BuildValue("s#",
  222.                          (char *)&theKeys__out__, (int)sizeof(KeyMap));
  223.  theKeys__error__: ;
  224.     return _res;
  225. }
  226.  
  227. static PyObject *Evt_TickCount(_self, _args)
  228.     PyObject *_self;
  229.     PyObject *_args;
  230. {
  231.     PyObject *_res = NULL;
  232.     UInt32 _rv;
  233.     if (!PyArg_ParseTuple(_args, ""))
  234.         return NULL;
  235.     _rv = TickCount();
  236.     _res = Py_BuildValue("l",
  237.                          _rv);
  238.     return _res;
  239. }
  240.  
  241. static PyObject *Evt_PostEvent(_self, _args)
  242.     PyObject *_self;
  243.     PyObject *_args;
  244. {
  245.     PyObject *_res = NULL;
  246.     OSErr _err;
  247.     EventKind eventNum;
  248.     UInt32 eventMsg;
  249.     if (!PyArg_ParseTuple(_args, "hl",
  250.                           &eventNum,
  251.                           &eventMsg))
  252.         return NULL;
  253.     _err = PostEvent(eventNum,
  254.                      eventMsg);
  255.     if (_err != noErr) return PyMac_Error(_err);
  256.     Py_INCREF(Py_None);
  257.     _res = Py_None;
  258.     return _res;
  259. }
  260.  
  261. static PyObject *Evt_OSEventAvail(_self, _args)
  262.     PyObject *_self;
  263.     PyObject *_args;
  264. {
  265.     PyObject *_res = NULL;
  266.     Boolean _rv;
  267.     EventMask mask;
  268.     EventRecord theEvent;
  269.     if (!PyArg_ParseTuple(_args, "h",
  270.                           &mask))
  271.         return NULL;
  272.     _rv = OSEventAvail(mask,
  273.                        &theEvent);
  274.     _res = Py_BuildValue("bO&",
  275.                          _rv,
  276.                          PyMac_BuildEventRecord, &theEvent);
  277.     return _res;
  278. }
  279.  
  280. static PyObject *Evt_GetOSEvent(_self, _args)
  281.     PyObject *_self;
  282.     PyObject *_args;
  283. {
  284.     PyObject *_res = NULL;
  285.     Boolean _rv;
  286.     EventMask mask;
  287.     EventRecord theEvent;
  288.     if (!PyArg_ParseTuple(_args, "h",
  289.                           &mask))
  290.         return NULL;
  291.     _rv = GetOSEvent(mask,
  292.                      &theEvent);
  293.     _res = Py_BuildValue("bO&",
  294.                          _rv,
  295.                          PyMac_BuildEventRecord, &theEvent);
  296.     return _res;
  297. }
  298.  
  299. static PyObject *Evt_FlushEvents(_self, _args)
  300.     PyObject *_self;
  301.     PyObject *_args;
  302. {
  303.     PyObject *_res = NULL;
  304.     EventMask whichMask;
  305.     EventMask stopMask;
  306.     if (!PyArg_ParseTuple(_args, "hh",
  307.                           &whichMask,
  308.                           &stopMask))
  309.         return NULL;
  310.     FlushEvents(whichMask,
  311.                 stopMask);
  312.     Py_INCREF(Py_None);
  313.     _res = Py_None;
  314.     return _res;
  315. }
  316.  
  317. static PyObject *Evt_SystemClick(_self, _args)
  318.     PyObject *_self;
  319.     PyObject *_args;
  320. {
  321.     PyObject *_res = NULL;
  322.     EventRecord theEvent;
  323.     WindowPtr theWindow;
  324.     if (!PyArg_ParseTuple(_args, "O&O&",
  325.                           PyMac_GetEventRecord, &theEvent,
  326.                           WinObj_Convert, &theWindow))
  327.         return NULL;
  328.     SystemClick(&theEvent,
  329.                 theWindow);
  330.     Py_INCREF(Py_None);
  331.     _res = Py_None;
  332.     return _res;
  333. }
  334.  
  335. static PyObject *Evt_SystemTask(_self, _args)
  336.     PyObject *_self;
  337.     PyObject *_args;
  338. {
  339.     PyObject *_res = NULL;
  340.     if (!PyArg_ParseTuple(_args, ""))
  341.         return NULL;
  342.     SystemTask();
  343.     Py_INCREF(Py_None);
  344.     _res = Py_None;
  345.     return _res;
  346. }
  347.  
  348. static PyObject *Evt_SystemEvent(_self, _args)
  349.     PyObject *_self;
  350.     PyObject *_args;
  351. {
  352.     PyObject *_res = NULL;
  353.     Boolean _rv;
  354.     EventRecord theEvent;
  355.     if (!PyArg_ParseTuple(_args, "O&",
  356.                           PyMac_GetEventRecord, &theEvent))
  357.         return NULL;
  358.     _rv = SystemEvent(&theEvent);
  359.     _res = Py_BuildValue("b",
  360.                          _rv);
  361.     return _res;
  362. }
  363.  
  364. static PyMethodDef Evt_methods[] = {
  365.     {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
  366.      "() -> (UInt32 _rv)"},
  367.     {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
  368.      "(EventMask value) -> None"},
  369.     {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
  370.      "() -> (UInt32 _rv)"},
  371.     {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
  372.      "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
  373.     {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
  374.      "(EventMask eventMask, UInt32 sleep) -> (Boolean _rv, EventRecord theEvent)"},
  375.     {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
  376.      "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
  377.     {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
  378.      "() -> (Point mouseLoc)"},
  379.     {"Button", (PyCFunction)Evt_Button, 1,
  380.      "() -> (Boolean _rv)"},
  381.     {"StillDown", (PyCFunction)Evt_StillDown, 1,
  382.      "() -> (Boolean _rv)"},
  383.     {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
  384.      "() -> (Boolean _rv)"},
  385.     {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
  386.      "() -> (KeyMap theKeys)"},
  387.     {"TickCount", (PyCFunction)Evt_TickCount, 1,
  388.      "() -> (UInt32 _rv)"},
  389.     {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
  390.      "(EventKind eventNum, UInt32 eventMsg) -> None"},
  391.     {"OSEventAvail", (PyCFunction)Evt_OSEventAvail, 1,
  392.      "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
  393.     {"GetOSEvent", (PyCFunction)Evt_GetOSEvent, 1,
  394.      "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
  395.     {"FlushEvents", (PyCFunction)Evt_FlushEvents, 1,
  396.      "(EventMask whichMask, EventMask stopMask) -> None"},
  397.     {"SystemClick", (PyCFunction)Evt_SystemClick, 1,
  398.      "(EventRecord theEvent, WindowPtr theWindow) -> None"},
  399.     {"SystemTask", (PyCFunction)Evt_SystemTask, 1,
  400.      "() -> None"},
  401.     {"SystemEvent", (PyCFunction)Evt_SystemEvent, 1,
  402.      "(EventRecord theEvent) -> (Boolean _rv)"},
  403.     {NULL, NULL, 0}
  404. };
  405.  
  406.  
  407.  
  408.  
  409. void initEvt()
  410. {
  411.     PyObject *m;
  412.     PyObject *d;
  413.  
  414.  
  415.  
  416.  
  417.     m = Py_InitModule("Evt", Evt_methods);
  418.     d = PyModule_GetDict(m);
  419.     Evt_Error = PyMac_GetOSErrException();
  420.     if (Evt_Error == NULL ||
  421.         PyDict_SetItemString(d, "Error", Evt_Error) != 0)
  422.         Py_FatalError("can't initialize Evt.Error");
  423. }
  424.  
  425. /* ========================= End module Evt ========================= */
  426.  
  427.